/**
 * Copyright (c) 快宝网络 kuaidihelp.com Co., Ltd. All Rights Reserved 禁止外泄以及用于其它的商业用途
 */

import React, { useState } from 'react';
import { Input, message, Row, Col, Typography } from 'antd';
import { runSql } from '@/services/tools/runSql';
import { PageContainer } from '@ant-design/pro-layout';
import styles from './index.less';

const { Search } = Input;
const { Paragraph } = Typography;

const Index = () => {
  const [loading, setLoading] = useState<boolean>(false);
  const [dataSource, setDataSource] = useState<any>({});
  const columns = [
    {
      title: '执行的sql',
      dataIndex: 'sql',
    },
    {
      title: '执行结果',
      dataIndex: 'result',
    },
    {
      title: '说明',
      dataIndex: 'message',
    },
    {
      title: '可能影响行数',
      dataIndex: 'effect_rows',
    },
    {
      title: '实际影响行数',
      dataIndex: 'real_effect_rows',
    },
  ];
  const onFinish = (sql: string) => {
    setLoading(true);
    runSql({ sql }).then((res) => {
      setLoading(false);
      const { code, data, msg } = res;
      if (code == 0) {
        setDataSource(data);
      } else {
        message.error(msg);
      }
    });
  };

  return (
    <PageContainer className={styles.main}>
      <Search
        placeholder='请输入sql语句'
        enterButton='执行'
        onSearch={onFinish}
        size='large'
        loading={loading}
      />
      <Row gutter={[20, 20]} style={{ marginTop: 24 }}>
        {columns.map((item) => {
          const { title, dataIndex } = item;
          return (
            <>
              <Col span={2} xs={24} sm={24}>
                {title}：
              </Col>
              <Col span={22} xs={24} sm={24}>
                {dataIndex === 'result' ? (
                  dataSource[dataIndex] === '1' ? (
                    <Paragraph type='success' style={{ margin: 0 }}>
                      成功
                    </Paragraph>
                  ) : dataSource[dataIndex] === '0' ? (
                    <Paragraph type='danger' style={{ margin: 0 }}>
                      失败
                    </Paragraph>
                  ) : (
                    dataSource[dataIndex]
                  )
                ) : (
                  dataSource[dataIndex]
                )}
              </Col>
            </>
          );
        })}
      </Row>
    </PageContainer>
  );
};

export default Index;
